package ru.compft.common.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import ru.compft.common.dao.UserDao;
import ru.compft.common.data.AppUser;
import ru.compft.common.data.meta.UserProperty;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: ASUS
 * Date: 01.06.12
 * Time: 0:06
 * DAO работы с пользователями
 */
@Repository
public class UserDaoImpl implements UserDao {
    private static final Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public AppUser findUserByLogin(String userName) {
        System.out.println("UserDao findUserByLogin userName = " + userName);
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppUser.class)
                .add(Restrictions.like(UserProperty.login, userName));

        return (AppUser) criteria.uniqueResult();
    }

    @Override
    public AppUser findUserById(Long userId) {
        System.out.println("UserDao findUserById userId = " + userId);
        Session session = sessionFactory.getCurrentSession();
        return (AppUser) session.get(AppUser.class, userId);
    }

    @Override
    public List<AppUser> getAllUsers() {
        System.out.println("UserDao getAllUsers ");
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppUser.class);

        return criteria.list();
    }

    @Override
    public List<AppUser> getUsers(int from, int count) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppUser.class)
                .setFirstResult(from)
                .setMaxResults(count);

        return criteria.list();
    }

    @Override
    public int countAllUsers() {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppUser.class)
                .setProjection(Projections.rowCount());

        final Object result = criteria.uniqueResult();
        return result != null ? Integer.parseInt(result.toString()) : 0;
    }

    @Override
    public AppUser persist(AppUser user) {
        System.out.println("UserDao addUser ");
        sessionFactory.getCurrentSession().persist(user);
        return user;
    }

    @Override
    public Boolean delete(AppUser user) {
        System.out.println("UserDao delete ");
        sessionFactory.getCurrentSession().delete(user);
        return true;
    }

    @Override
    public AppUser update(AppUser user) {
        System.out.println("UserDao update ");
        return (AppUser) sessionFactory.getCurrentSession().merge(user);
    }
}
